<?php
/**
 * 登录控制器
 * @Author    紫梦
 * @DateTime  2017-11-18
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\wxapp\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\common\model\RolesNode;
use app\common\request\Curl;
use app\common\request\wxapp\WxBizDataCrypt;

class Login extends controller
{
	public function login()
	{
		$request = Request::instance();
		$data = $request->post();
		$openId = self::wxLogin($data['code']);

		if(empty($openId['openid'])){
			return $openId;
		}

		if(empty($data['password']) || empty($data['user_name'])){
			$admin = Db::name('admin')->field('id,agency_id,role_id')->where(['wp_openid'=>$openId['openid'],'status'=>1])->find();
			if($admin){
				$rolesNode = RolesNode::where(['role_id'=>$admin['role_id']])->select();
				if($rolesNode){
					foreach ($rolesNode as $value) {
						$nodes['nodes'][] = strtolower($value['nodes']['node_value']);
					}					
				}else{
					$nodes['nodes'][] = [];
				}
				$session = array_merge($admin,$nodes);
				session('admin',$session);
				return [
						'code' => 1,
						'msg' => '登录成功',
						'data'=>[
							'PHPSESSID'=>session_id(),
							'nodes' => $nodes['nodes']
							]
						];
			}else{
				return [
					'code' => 10001,
					'msg' => '未绑定微信号'
					];
			}
		}else{
			$admin = Db::name('admin')->field('id,password,agency_id,role_id')->where(['user_name'=>$data['user_name'],'status'=>1])->find();
			Db::name('admin')
				->where(['id'=>$admin['id'],'status'=>1])
				->update(['wp_openid' => $openId['openid']]);
			if($admin){
				$pass = password_verify($data['password'],$admin['password']);
				if($pass){
					unset($admin['password']);
					$rolesNode = RolesNode::where(['role_id'=>$admin['role_id']])->select();
					if($rolesNode){
						foreach ($rolesNode as $value) {
							$nodes['nodes'][] = strtolower($value['nodes']['node_value']);
						}					
					}else{
						$nodes['nodes'][] = [];
					}
					$session = array_merge($admin,$nodes);
					session('admin',$session);

					return [
						'code' => 1,
						'msg' => '登录成功',
						'data'=>[
							'PHPSESSID'=>session_id(),
							'nodes' => $nodes['nodes']
							]
						];
				}else{
					return [
						'code'=> 10002,
						'msg'=> "密码错误"
						];
				}
			}else{
				return [
					'code' => 10002,
					'msg' => '账号不存在'
					];
			}
		}
	}

	/**
	 * 请求微信APi
	 */
	private static function wxLogin($jscode)
	{
		// 微信登录api
		$appid = 'wx9e103dd9205636fa';
		$secret = 'e4989b418af99197d3d15595ffc29217';
		$apiUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$jscode.'&grant_type=authorization_code';

		$curl = new Curl;
		$curl->url($apiUrl);

		// 任务结果状态
		if ($curl->error()) {
		    echo $curl->message();
		} else {
		    // 任务进程信息
		    $info = $curl->info();
		    
		    // 任务结果内容
		    $content = $curl->data();
		}

		return json_decode($content,true);
				
	}


	public function loginOut()
	{
		//退出登录
		session(null);
		return [
			'code' => 1,
			'msg' => '退出成功'
			];
	}
}